const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        userName: '',
        password: ''
    },
    getInputValue(e) {
        const item = e.currentTarget.dataset.item
        this.setData({
            [item]: e.detail.value
        })
    },
    check() {
        let errorMsg = '',
            flag = false;
        if (!this.data.userName) {
            errorMsg = '请输入用户名';
        } else if (!this.data.password) {
            errorMsg = '请输入密码';
        } else {
            flag = true;
        }
        if (!flag) {
            wx.showToast({
                title: errorMsg,
                icon: 'none',
            })
        }
        console.log('确认信息')
        console.log(flag)
        return flag;
    },
    // 登录
    login() {
      console.log('点击了')
        if (this.check()) {
            app.fetchRequest({
                url: '/user/login',
                data: {
                    "loginId": this.data.userName,
                    "password": this.data.password
                },
                success: (res) => {
                    wx.setStorageSync('token', res.token);
                    wx.setStorageSync('userInfo', res);
                    wx.switchTab({
                      url: '../index/index',
                    })
                    //wx.navigateBack();
                },
                fail: (msg) => {
                    wx.showToast({
                        title: msg,
                        icon: 'none',
                    })
                }
            })
        }
    }
})